/**
 <p>选择gridpanel控件</p>
 * xtype = cceafselectgridpanel

 * @module Ext-ux-cceaf-control
 * @class cceaf.control.GridPanel
 * @extends Ext.grid.Panel
 * @since 1.0
 */
Ext.define('cceaf.control.SelectGridPanel',{
	extend: 'Ext.grid.Panel', 
    alias: 'widget.cceafselectgridpanel',

	constructor: function(config){
		config = config || {};
		
		cceaf.control.SelectGridPanel.superclass.constructor.apply(this, arguments);
	},

	/**
	 * 模块名称，在tree的title上显示
	 *
     * @property moduleName 
     * @type String
     * @default null
     */
	moduleName: "",
	
	/**
	 * 列表model
	 *
     * @property model 
     * @type String
     * @default null
     */
	model: null,
	
	/**
	 * record选择模式,可用的值包括：
	 * -SINGLE , 单选,一次只允许选择一条记录
	 * -SIMPLE , 简单的多选,每点一次选择一条(默认)
	 * -MULTI , 复杂的多选,可配合ctrl,shift进行多选
     * @property model 
     * @type String
     * @default SIMPLE
     */
	selectMode: 'SIMPLE',
	
	/**
	 * 查询的action
	 *
     * @property queryAction 
     * @type String
     * @default null
     */
	queryAction: null,
	
	/**
	 * 主键名称集合，以,分开
	 *
     * @property masterKeyIds 
     * @type String
     * @default null
     */
	masterKeyIds: null,
	
	/**
	 * 是否显示基础的功能按钮
	 *
     * @property showBaseToolbar 
     * @type boolean
     * @default true
     */
	showBaseToolbar: true,
	
	headerCheckbox: false,
	/**
	 * 额外添加(自定义)的功能按钮
	 *
     * @property operateToolbar 
     * @type array
     * @default null
     */
	operateToolbar: null,
	
	/**
	 * 弹出窗口大小，默认如下：
	 * <br> popWindow: {
	 * <br> 	width: 530,
	 * <br> 	heigth: 400
	 * <br> }
	 *
     * @property popWindow 
     * @type attribute
     * @default null
     */
	popWindow: {
		width: 530,
		heigth: 400
	},
	
	/**
	 * 是否为card布局显示
	 *
     * @property cardLayout 
     * @type attribute Boolean
     * @default false
     */
	cardLayout: false,
	
	/**
	 * 设置每行的class
	 */
	viewConfig: {
		forceFit:true,
		enableRowBody:true,
		getRowClass : function(record, rowIndex, p, ds){
			return '';
		}
	},
	
	/**
	 * dataStore分组字段
	 * @property groupField 
     * @type String
     * @default null
	 */
	groupField: null,
	
	initComponent : function(){
		var config = this;

		var columns = config.columns;
		if (!columns) {
			cceaf.util.ExtHelper.showErrorDialog("必须设置grid的columns！");
		}

		var queryAction = config.queryAction;
		if(!queryAction) {
			cceaf.util.ExtHelper.showErrorDialog("请设置stroe加载的url！");
		}
		
		var groupField = null;
		if (config.groupField) {
			groupField = config.groupField;
		}
		
		//定义数据源
	    var ds = new Ext.data.Store({
	    	autoLoad: false,
	    	proxy: {
	    		type: 'ajax',
	    		url:  queryAction,
	    		actionMethods: {  
		            read: 'POST'  
		        }
			},
			groupField: groupField,
	        autoDestroy: true,  
	    	model: config.model
	    });
	    //全选
	    function SelectAll(){
	    	var config = this.up('gridpanel');
	    	var selModel = config.getSelectionModel();
	    	//selModel.deselectAll();
	    	selModel.selectAll();
	    };
	    //全不选
	    function UnSelectAll(){
	    	var config = this.up('gridpanel');
	    	var selModel = config.getSelectionModel();
	    	selModel.deselectAll();
	    };
	    //反选
	    function DeSelect(){
	    	var config = this.up('gridpanel');
	    	var selModel = config.getSelectionModel();
	    	var selRecord = selModel.getSelection();
	    	//selModel.deselectAll();
	    	selModel.selectAll();
	    	selModel.deselect(selRecord);
	    };
	    /**
		 * 默认操作区
		 */
		var operateArea = [],i=0;
		if(config.showBaseToolbar){
			operateArea[i] = {itemId: 'btnSelectAll', text: ' 全选 ', iconCls: 'page_addIcon', pressed: true, handler: SelectAll};
			i++;
			operateArea[i] = '-';
			i++;
			operateArea[i] = {itemId: 'btnUnSelectAll', text: ' 全不选 ', iconCls: 'page_editIcon', pressed: true, handler: UnSelectAll};
			i++;
			operateArea[i] = '-';
			i++;
			operateArea[i] = {itemId: 'btnDeSelect', text: ' 反选 ', iconCls: 'page_viewIcon', pressed: true, handler: DeSelect};
			i++;
			operateArea[i] = '-';
			i++;
		}
		var operateToolbar = config.operateToolbar;
		var toolbar;
		if(operateToolbar){
			var array = operateArea.concat(operateToolbar);
			toolbar = {
				//dock: 'top', 
				xtype: 'toolbar',
				items: array
			};
		} else {
			toolbar = {
				//dock: 'top', 
				xtype: 'toolbar',
				items: operateArea
			};
		}
		
		var title = '';
		if (config.moduleName) {
			title = config.moduleName + '信息';
		}
		
		var height = config.height;
		var width = config.width;
		if(config.height == null){
			height = 150;
		}
		
		var selModel = Ext.create('Ext.selection.CheckboxModel',{
			showHeaderCheckbox: config.headerCheckbox,
			mode: config.selectMode?config.selectMode:'SIMPLE',
			//checkOnly: true,
			ignoreRightMouseSelection: true
		});

		Ext.applyIf(this, {
			store: ds,
			title: title,
	        border: true,
	        frame: true,
	        stripeRows : true,
	        height: height,
	        width: width,
	        selModel: selModel,
	        loadMask: {msg:'正在加载数据，请稍侯……'},
	        dockedItems: toolbar
		});
		
		cceaf.control.SelectGridPanel.superclass.initComponent.call(this);
    }
    
});